#include <QDebug>
#include <QDir>
#include <QApplication>
#include <QSettings>
#include <QStringBuilder>
#include "AddNewItem.h"
#include "Node.h"
#include "Include/Project.h"
#include "CreateItems/CreateClass.h"
#include "CreateItems/CreateFile.h"
#include "CreateItems/CreateModule.h"
#include "CreateItems/CreateClassForm.h"
#include "ui_AddNewItem.h"


AddNewItem::AddNewItem(Node * node, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddNewItem),
    _node(node)
{
    ui->setupUi(this);

    _groups[Python] = tr("Python");
    _groups[PyQt]   = tr("Python Qt");
    _groups[QML]    = tr("QML");
    _groups[Other]  = tr("Other");

    _items[Python] = QHash<Items, QString>();
    _items[Python][Class]   = tr("Python class");
    _items[Python][File]    = tr("Empty python file");
    _items[Python][Module]  = tr("Python module");

    _items[PyQt] = QHash<Items, QString>();
    _items[PyQt][ClassForm] = tr("Python Qt Designer class form");
    _items[PyQt][Form]      = tr("Qt Designer form");
    _items[PyQt][Resource]  = tr("Qt Resource file");

    _items[QML] = QHash<Items, QString>();
    _items[QML][QMLItem]    = tr("QML file");
    _items[QML][Js]         = tr("Javascript file");

    _items[Other] = QHash<Items, QString>();
    _items[Other][AnyFile]     = tr("File");
    _items[Other][Dir]      = tr("Directory");

    _descr[Class] = tr("Simple python class");
    _descr[File] = tr("Simple python file");
    _descr[Module] = tr("Empty python module");
    _descr[ClassForm] = tr("Qt class with connected Qt designer form");
    _descr[Form] = tr("Qt designer form");
    _descr[Resource] = tr("Qt resource file");
    _descr[QMLItem] = tr("QML empty form");
    _descr[Js] = tr("JavaScript file");
    _descr[AnyFile] = tr("Empty file");
    _descr[Dir] = tr("Empty directory");

    if (Plugins::Project::currentInstance()->guiVersion() == ""){
        _disabled[PyQt] = true;
        _disabled[QML] = true;
    }

    foreach(Groups item, _groups.keys()){
        QListWidgetItem *w = new QListWidgetItem(_groups[item], ui->groupsWidget);
        w->setData(Qt::UserRole, item);
        if (_disabled.contains(item))
            w->setFlags(Qt::ItemIsSelectable);
    }

    connect(ui->groupsWidget, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), SLOT(groupChanged(QListWidgetItem*,QListWidgetItem*)));
    connect(ui->itemsWidget, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), SLOT(itemChanged(QListWidgetItem*,QListWidgetItem*)));
    ui->groupsWidget->setCurrentRow(0);

    connect(ui->buttonBox, SIGNAL(rejected()), SLOT(reject()));
    connect(ui->buttonBox, SIGNAL(accepted()), SLOT(onAccept()));
}

AddNewItem::~AddNewItem()
{
    delete ui;
}

void AddNewItem::groupChanged(QListWidgetItem * item, QListWidgetItem*)
{
    ui->itemsWidget->clear();
    ui->textBrowser->clear();
    Groups g = (Groups)(item->data(Qt::UserRole).toInt());
    foreach(Items it, _items[g].keys()){
        QListWidgetItem *w = new QListWidgetItem(_items[g][it], ui->itemsWidget);
        w->setData(Qt::UserRole, it);
    }
    ui->itemsWidget->setCurrentRow(0);
}

void AddNewItem::itemChanged(QListWidgetItem * item, QListWidgetItem*)
{
    if (item){
        Items it = (Items)(item->data(Qt::UserRole).toInt());
        ui->textBrowser->setHtml(_descr[it]);
    }
}

void AddNewItem::onAccept()
{
    Items what = (Items)ui->itemsWidget->currentIndex().data(Qt::UserRole).toInt();
    switch(what){
    case Class: createClass(); break;
    case File: createPyFile(); break;
    case Module: createModule(); break;
    case ClassForm: createClassForm(); break;
    case Form: break;
    case Resource: break;
    case QMLItem: break;
    case Js: break;
    case AnyFile: break;
    case Dir: break;
    }
}

void AddNewItem::createClass()
{
    CreateClass dial(_node->path(), this);
    if (dial.exec() == QDialog::Accepted){
        _openAfter << dial.openAfter();
        accept();
    }
}

void AddNewItem::createPyFile()
{
    CreateFile file(_node->path(), this);
    if (file.exec() == QDialog::Accepted){
        _openAfter << file.openAfter();
        accept();
    }
}

void AddNewItem::createModule()
{
    CreateModule file(_node->path(), this);
    if (file.exec() == QDialog::Accepted){
        _openAfter << file.openAfter();
        accept();
    }
}

void AddNewItem::createClassForm()
{
    CreateClassForm file(_node->path(), this);
    if (file.exec() == QDialog::Accepted){
        _openAfter << file.openAfter();
        accept();
    }
}

QStringList AddNewItem::openAfter()
{
    return _openAfter;
}

